package com.webserver.servlet;

import com.webserver.http.HttpRequest;
import com.webserver.http.HttpResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.RandomAccessFile;

public class ToUpdateServlet {
    public void service(HttpRequest request, HttpResponse response){
        System.out.println("正在修改……");
       try (RandomAccessFile raf=new RandomAccessFile("user.dat","rw");){
           String username=request.getParameter("username").trim();
           for (int i=0;i<raf.length()/100;i++){
               raf.seek(i*100);
               byte[]data=new byte[32];
               raf.read(data);
               String name=new String(data,"utf-8").trim();
               if (name.equals(username)){
                   raf.read(data);
                   String password=new String(data,"utf-8").trim();
                   raf.read(data);
                   String nickname=new String(data,"utf-8").trim();
                   int age=raf.readInt();
                   PrintWriter pw=response.getWriter();
                   pw.println("<!DOCTYPE html>");
                   pw.println("<html lang=\"en\">");
                   pw.println("<head>");
                   pw.println("<meta charset=\"UTF-8\">");
                   pw.println("<title>\"修改\"</title>");
                   pw.println("</head>");
                   pw.println("<body>");
                   pw.println("<center>");
                   pw.println("<h1>修改用户信息</h1>");
                   pw.println("<form action=\"./updateUser\" method=\"post\">");
                   pw.println("<table border=\"3\">");
                   pw.println("<tr>");
                   pw.println("<td>用户名</td>");
                   pw.println("<td>");
                   pw.println("<input type=\"text\"value=\""+username+"\">");
                   pw.println("</td>");
                   pw.println("</tr>");

                   pw.println("<tr>");
                   pw.println("<td>密码</td>");
                   pw.println("<td>");
                   pw.println("<input type=\"password\"value=\""+password+"\">");
                   pw.println("</td>");
                   pw.println("</tr>");

                   pw.println("<tr>");
                   pw.println("<td>昵称</td>");
                   pw.println("<td>");
                   pw.println("<input type=\"text\"value=\""+nickname+"\">");
                   pw.println("</td>");
                   pw.println("</tr>");

                   pw.println("<tr>");
                   pw.println("<td>年龄</td>");
                   pw.println("<td>");
                   pw.println("<input type=\"text\"value=\""+age+"\">");
                   pw.println("</td>");
                   pw.println("</tr>");

                   pw.println("<tr>");
                   pw.println("<td colspan=\"2\" align=\"center\">");
                   pw.println("<input type=\"submit\"value=\"修改\">");
                   pw.println("</td>");
                   pw.println("</tr>");
                   pw.println("</table>");
                   pw.println("</center>");
                   pw.println("</body>");
                   pw.println("</html>");
                   return;
               }

           }

       }catch (IOException e){
           e.printStackTrace();
       }


        response.setContentType("text/html");
        System.out.println("修改成功！！！");
    }
}
